package cn.iocoder.yudao.module.product.service.merchant;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO;
import cn.iocoder.yudao.module.infra.dal.mysql.file.FileMapper;
import cn.iocoder.yudao.module.product.controller.app.merchant.vo.MerchantInformationRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.merchant.MerchantDO;
import org.springframework.stereotype.Service;
import cn.iocoder.yudao.module.system.dal.mysql.merchant.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 梁富贵
 * @Date 2024/10/15
 **/
@Service
public class MerchantCenterServiceImpl implements MerchantCenterService{

    private final List<String> MERCHANT_CENTER_BANNER = Collections.unmodifiableList(
            Arrays.asList("merchant_center_banner", "merchant_center_scenery", "merchant_center_scenery_video")
    );

    @Resource
    private FileMapper fileMapper;

    @Resource
    private MerchantMapper merchantMapper;

    /**
     * 获取商家中心信息
     * @return 商家中心信息
     */
    @Override
    public MerchantInformationRespVO getMerchantCenterInformation() {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        if(loginUser==null){
            return null;
        }
        return this.convert(this.getFileDOList(loginUser.getId()), this.getMerchantDOList(loginUser.getId()));
    }

    /**
     * 转换为商家中心信息
     * @param fileDOList 文件信息
     * @param merchantDOList 商家信息
     * @return 商家中心信息
     */
    private MerchantInformationRespVO convert(List<FileDO> fileDOList, MerchantDO merchantDOList){
        MerchantInformationRespVO respVO = new MerchantInformationRespVO();
        if(merchantDOList != null){
            respVO.setName(merchantDOList.getMerchantName());
            respVO.setAddress(merchantDOList.getContactAddress());
            respVO.setEmail(merchantDOList.getContactEmail());
            respVO.setPhone(merchantDOList.getContactPhone());
            respVO.setDescription(merchantDOList.getDescription());
        }
        if(fileDOList != null){
            respVO.setBanner(new ArrayList<>());
            respVO.setScenery(new ArrayList<>());
            respVO.setSceneryVideo(new ArrayList<>());
            for (FileDO fileDO : fileDOList) {
                if("merchant_center_banner".equals(fileDO.getFileType())){
                    respVO.getBanner().add(fileDO.getUrl());
                }
                if("merchant_center_scenery".equals(fileDO.getFileType())){
                    respVO.getScenery().add(fileDO.getUrl());
                }
                if("merchant_center_scenery_video".equals(fileDO.getFileType())){
                    respVO.getSceneryVideo().add(fileDO.getUrl());
                }
            }
        }
        return  respVO;
    }

    /**
     * 获取商家中心文件信息
     * @param userId 用户id
     * @return 文件信息
     */
    private  List<FileDO> getFileDOList(Long userId){
        LambdaQueryWrapperX<FileDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(FileDO::getCreator, userId);
        queryWrapper.inIfPresent(FileDO::getFileType, MERCHANT_CENTER_BANNER);
        if(userId  == null){
            return null;
        }
        return fileMapper.selectList(queryWrapper);
    }

    /**
     * 获取商家中心商家信息
     * @param userId 用户id
     * @return 商家信息
     */
    private MerchantDO getMerchantDOList(Long userId){
        LambdaQueryWrapperX<MerchantDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(MerchantDO::getCreator, userId);
        if(userId  == null){
            return null;
        }
        return merchantMapper.selectOne(queryWrapper);
    }
}
